package Netty_basic;

import io.netty.channel.EventLoop;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.DefaultPromise;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;

import java.util.concurrent.ExecutionException;

//
public class Element_promise {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
            Test3();
    }

    //同步处理结果
    public static void Test1() throws ExecutionException, InterruptedException {
        EventLoopGroup eventLoopGroup=new NioEventLoopGroup();
        EventLoop next = eventLoopGroup.next();
        DefaultPromise<Integer> promise=new DefaultPromise<>(next);

        next.submit(()->{
            try {
                System.out.println("working");
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("success!");
            promise.setSuccess(100);
        });

        Integer integer = promise.get();
        System.out.println(integer);
    }

    //异步处理结果
    public static void Test2() throws ExecutionException, InterruptedException {
        EventLoopGroup eventLoopGroup=new NioEventLoopGroup();
        EventLoop next = eventLoopGroup.next();
        DefaultPromise<Integer> promise=new DefaultPromise<>(next);

        next.submit(()->{
            try {
                System.out.println("working");
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("success!");
            promise.setSuccess(100);
        });

        //这个方法并不会阻塞当前线程，所以sout会执行，在promise中有结果时这个方法会执行，输出结果，所以是异步的
        promise.addListener(new GenericFutureListener<Future<? super Integer>>() {
            @Override
            public void operationComplete(Future<? super Integer> future) throws Exception {
                Object object = future.get();
                if (object instanceof Integer)
                    System.out.println((int)object);
            }
        });

        System.out.println("running--");
    }

    //异步处理结果（失败）
    public static void Test3() throws ExecutionException, InterruptedException {
        EventLoopGroup eventLoopGroup=new NioEventLoopGroup();
        EventLoop next = eventLoopGroup.next();
        DefaultPromise<Integer> promise=new DefaultPromise<>(next);

        next.submit(()->{
            try {
                System.out.println("working");
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("fail!");
            promise.setFailure(new Error("error!!!!"));
        });

        //这个方法并不会阻塞当前线程，所以sout会执行，在promise中有结果时这个方法会执行，输出结果，所以是异步的
        promise.addListener(new GenericFutureListener<Future<? super Integer>>() {
            @Override
            public void operationComplete(Future<? super Integer> future) throws Exception {
                boolean success = future.isSuccess();
                if (!success)
                    System.out.println(future.cause().toString());
                else
                    System.out.println(future.getNow());
            }
        });

        //System.out.println("running--");
    }
}
